/**
 * Created by Administrator on 2017/1/17.
 */
var Log = module.exports;
var DBFactory = require('../db/mysql_db.js');


Log.userLog = function (data, cb) {
    var sql = '';
    if(data.isAdmin){
        sql += 'insert into admin_log set ?';
    }else {
        sql += 'insert into user_log set ?';
    }
    DBFactory.poolQuery(sql,
        {
            uid: data.uid,
            account: data.account,
            detail: data.detail,
            type: data.type
        },
        function () {
            cb("no connection");
        },
        function (err, result) {
            if (err) {
                console.log('用户日志错误: ' + err);
                cb(false);
                return;
            }
            if (result) {
                cb(true, result);
            }
            return;
        });
}

Log.adminLog = function (data, cb) {
    DBFactory.poolQuery('insert into admin_log set ?',
        {
            uid: data.uid,
            adm_uid: data.adm_uid,
            account: data.account,
            detail: data.detail,
            type: data.type,
            app_sid:data.app_sid
        },
        function () {
            cb("no connection");
        },
        function (err, result) {
            if (err) {
                console.log('管理员日志错误: ' + err);
                cb(false);
                return;
            }
            if (result) {
                cb(true, result);
            }
        });
}


//isUserOrAdm:查看的是user_log表还是admin_log表；  true:user_log   false:admin_log
Log.logInfo = function (data,cb) {

    var sql;
    var value = [];

    if (data.isUserOrAdm == true || data.isUserOrAdm == 'true') {//查看的是user表
        sql = 'select uid,account,created_date,type,detail from user_log where type = ? ';
        value.push(data.type);

    } else {//查看的是admin表

        if(data.resetPwd){
            sql = 'select uid,adm_uid,account,created_date,type,detail,app_sid from admin_log where type >= 2 and type <= 3 ';
        }else {
            sql = 'select uid,adm_uid,account,created_date,type,detail,app_sid from admin_log where type = ? ';
            value.push(data.type);
        }

        if (data.app_sid) {
            sql += 'and app_sid = ? ';
            value.push(data.app_sid);
        }

    }

    if(data.account){
        sql += 'and account like ? ';
        value.push('%' + data.account + '%');
    }

    if(data.startTime && data.endTime){
        data.startTime += ' 00:00:00';
        data.endTime += ' 23:59:59';
        sql += 'and created_date >= ? and created_date <= ? ';
        value.push(data.startTime);
        value.push(data.endTime);
    }else if(data.startTime){
        var startTime = data.startTime + ' 00:00:00';
        var endTime = data.startTime + ' 23:59:59';
        sql += 'and created_date >= ? and created_date <= ? ';
        value.push(startTime);
        value.push(endTime);
    }

    var pageNum = data.pageNum ? data.pageNum : 10;
    var page = data.page ? data.page : 1;
    sql += 'limit ' + (page - 1)*pageNum + ',' + pageNum;

    DBFactory.poolQuery(sql, value,
        function () {
            cb("no connection");
        },
        function (err, result) {
            if (err) {
                cb(err,null);
                return;
            }
            if (result) {
                cb(null, result);
            }
        });
}


//isUserOrAdm:查看的是user_log表还是admin_log表；  true:user_log   false:admin_log
Log.vipLogInfo = function (data,cb) {

    var sql = "SELECT * FROM vip_log ";
    var value = [];

    if(data.app_sid){
        sql += "WHERE app_sid = ? ";
        value.push(data.app_sid);
    }
    if(data.recordType){
        sql += value.length > 0 ? "AND original_vip_name = ? " : "WHERE original_vip_name = ? ";
        value.push(data.recordType);
    }
    //这个搜索是给跑单的积分、押金类型用的，查具体的项目
    if(data.vip_sid){
        sql += value.length >0 ? "AND vip_sid = ? " : "WHERE vip_sid = ? ";
        value.push(data.vip_sid);
    }
    //查全部不传，查积分或者押金带上具体的vip_sid 以及 vip_type
    if(data.vip_type){
        sql += value.length >0 ? "AND vip_type = ? " : "WHERE vip_type = ? ";
        value.push(data.vip_type);
    }
    if(!data.alter){//查变动记录
        sql += value.length >0 ? "AND cost > ? " : "WHERE cost > ? ";
        value.push(0);
    }
    if(data.account){
        sql += value.length > 0 ? "AND client_account like ? " : "WHERE client_account like ? ";
        value.push('%' + data.account + '%');
    }

    if(data.startTime && data.endTime){
        data.startTime += ' 00:00:00';
        data.endTime += ' 23:59:59';
        sql += value.length > 0 ? 'AND created_date >= ? AND created_date <= ? ' : 'WHERE created_date >= ? AND created_date <= ? ';
        value.push(data.startTime);
        value.push(data.endTime);
    }else if(data.startTime){
        var startTime = data.startTime + ' 00:00:00';
        var endTime = data.startTime + ' 23:59:59';
        sql += value.length > 0 ? 'AND created_date >= ? AND created_date <= ? ' : 'WHERE created_date >= ? AND created_date <= ? ';
        value.push(startTime);
        value.push(endTime);
    }


    var pageNum = data.pageNum ? data.pageNum : 10;
    var page = data.page ? data.page : 1;
    sql += 'limit ' + (page - 1)*pageNum + ',' + pageNum;
    DBFactory.poolQuery(sql, value,
        function () {
            cb("no connection");
        },
        function (err, result) {
            if (err) {
                cb(err,null);
                return;
            }
            if (result) {
                result.forEach(function (o) {
                    if(o.created_date) o.created_date = simpleDateFormat(o.created_date);
                    if(o.original_vip_expire) o.original_vip_expire = simpleDateFormat(o.original_vip_expire);
                    if(o.vip_expire) o.vip_expire = simpleDateFormat(o.vip_expire);
                })
                cb(null, result);
            }
        });
}

//isUserOrAdm:查看的是user_log表还是admin_log表；  true:user_log   false:admin_log
Log.vipLogInfos = function (data,cb) {

    var sql = "SELECT a.*,b.count FROM vip_log a INNER JOIN (SELECT COUNT(1) count FROM vip_log";
    var value = [];
    if(data.app_sid){
        sql += " WHERE app_sid = ? ";
        value.push(data.app_sid);
    }
    if(data.recordType){
        if(data.recordType.indexOf('充值所得积分')<0){
            sql += value.length > 0 ? " AND original_vip_name = ? " : " WHERE original_vip_name = ? ";
            value.push(data.recordType);
        }else {
            sql += value.length > 0 ? " AND cost > 0 " : " WHERE cost > 0 ";
        }
    }
    //这个搜索是给跑单的积分、押金类型用的，查具体的项目
    if(data.vip_sid){
        sql += value.length >0 ? " AND vip_sid = ? " : " WHERE vip_sid = ? ";
        value.push(data.vip_sid);
    }
    if(data.account){
        sql += value.length > 0 ? " AND client_account like ? " : " WHERE client_account like ? ";
        value.push('%' + data.account + '%');
    }

    sql += ') b ON 1= 1';
    if(data.app_sid){
        sql += " WHERE a.app_sid = ? ";
        value.push(data.app_sid);
    }
    if(data.recordType){
        if(data.recordType.indexOf('充值所得积分')<0){
            sql += value.length > 0 ? " AND original_vip_name = ? " : " WHERE original_vip_name = ? ";
            value.push(data.recordType);
        }else {
            sql += value.length > 0 ? " AND cost > 0 " : " WHERE cost > 0 ";
        }
    }

    //这个搜索是给跑单的积分、押金类型用的，查具体的项目
    if(data.vip_sid){
        sql += value.length > 1 ? " AND a.vip_sid = ? " : " WHERE a.vip_sid = ? ";
        value.push(data.vip_sid);
    }
    if(data.account){
        sql += value.length > 1 ? " AND a.client_account like ? " : " WHERE a.client_account like ? ";
        value.push('%' + data.account + '%');
    }
    sql += ' order by a.created_date desc,a.id desc';
    // if(data.startTime && data.endTime){
    //     data.startTime += ' 00:00:00';
    //     data.endTime += ' 23:59:59';
    //     sql += value.length > 0 ? 'AND created_date >= ? AND created_date <= ? ' : 'WHERE created_date >= ? AND created_date <= ? ';
    //     value.push(data.startTime);
    //     value.push(data.endTime);
    // }else if(data.startTime){
    //     var startTime = data.startTime + ' 00:00:00';
    //     var endTime = data.startTime + ' 23:59:59';
    //     sql += value.length > 0 ? 'AND created_date >= ? AND created_date <= ? ' : 'WHERE created_date >= ? AND created_date <= ? ';
    //     value.push(startTime);
    //     value.push(endTime);
    // }


    var pageNum = data.pageNum ? data.pageNum : 10;
    var page = data.page ? data.page : 1;
    sql += ' limit ' + (page - 1)*pageNum + ',' + pageNum;
    console.log(sql);
    DBFactory.poolQuery(sql, value,
        function () {
            cb("no connection");
        },
        function (err, result) {
            if (err) {
                cb(err,null);
                return;
            }
            if (result) {
                var logs = {};
                if (result.length > 0) {
                    logs.totalNum = result[0].count;
                }
                logs.infos = result;
                cb(null, logs);
            }
        });
}



